package com.chen.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.blog.common.api.CommonResult;
import com.chen.blog.common.constant.MessageConstant;
import com.chen.blog.mapper.BlogTagMapper;
import com.chen.blog.model.Blog;
import com.chen.blog.model.BlogTag;
import com.chen.blog.model.Tag;
import com.chen.blog.mapper.TagMapper;
import com.chen.blog.service.TagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chen
 * @since 2022-01-24
 */
@Service
@Transactional
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private BlogTagMapper blogTagMapper;

    /**
     * 添加标签功能实现
     *
     * @param tag
     * @return
     */
    @Override
    public CommonResult add(Tag tag) {
        Tag realTag = tagMapper.selectOne(new QueryWrapper<Tag>().eq("tag_name", tag.getTagName()));
        if (realTag != null) {
            return CommonResult.error(MessageConstant.TAG_NAME_FAIL);
        }
        if (tagMapper.insert(tag) > 0) {
            return CommonResult.success(MessageConstant.ADD_POSITION_SUCCESS);
        }
        return CommonResult.success(MessageConstant.ADD_POSITION_FAIL);
    }

    /**
     * 批量删除标签功能实现
     *
     * @param ids
     * @return
     */
    @Override
    public CommonResult deleteByIds(Integer[] ids) {
        for (Integer id : ids) {
            blogTagMapper.delete(new QueryWrapper<BlogTag>().eq("tag_id", id));
        }
        if (tagMapper.deleteBatchIds(Arrays.asList(ids)) > 0) {
            return CommonResult.success(MessageConstant.DELETE_POSITION_SUCCESS);
        }
        return CommonResult.error(MessageConstant.DELETE_POSITION_FAIL);
    }

    /**
     * 更新标签功能实现
     *
     * @param tag
     * @return
     */
    @Override
    public CommonResult updateName(Tag tag) {
        Tag realTag = tagMapper.selectOne(new QueryWrapper<Tag>().eq("tag_name", tag.getTagName()));
        if (realTag != null) {
            return CommonResult.error(MessageConstant.TAG_NAME_FAIL);
        }
        tag.setUpdateTime(LocalDateTime.now());
        if (tagMapper.updateById(tag) > 0) {
            return CommonResult.success(MessageConstant.UPDATE_POSITION_SUCCESS);
        }
        return CommonResult.error(MessageConstant.UPDATE_POSITION_FAIL);
    }

    /**
     * 分页复合查询标签功能实现
     *
     * @param currentPage
     * @param pageSize
     * @param tag
     * @return
     */
    @Override
    public CommonResult getByParamAndPage(Integer currentPage, Integer pageSize, Tag tag) {
        Page<Tag> page = tagMapper.selectPage(new Page<>(currentPage, pageSize), new QueryWrapper<Tag>()
                .like(tag.getId() != null, "id", tag.getId())
                .like(!StringUtils.isEmpty(tag.getTagName()), "tag_name", tag.getTagName()));
        if (currentPage > page.getPages()) {
            return CommonResult.success(null, tagMapper.selectPage(new Page<>(page.getPages(), pageSize), new QueryWrapper<Tag>()
                    .like(tag.getId() != null, "id", tag.getId())
                    .like(!StringUtils.isEmpty(tag.getTagName()), "tag_name", tag.getTagName())));
        }
        return CommonResult.success(null, page);
    }

    /**
     * 获取指定标签下的博客功能实现
     *
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> getBlogByTag(Integer id) {
        return tagMapper.getBlogByTag(id);
    }

    /**
     * 获取指定标签和其下的博客数量功能实现
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getBlogCountByTag() {
        return tagMapper.getBlogCountByTag();
    }
}
